home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 August / CHIP_CD_2004-08.iso / software / amc / amc_install.exe / {app} / Scripts / IMDB (batch).ifs < prev    next >
Encoding:
Text File  |  2004-03-20  |  26.9 KB  |  875 lines

  1. // SCRIPTING
  2. // IMDB (US) automated batch importing (use Editor to change options)
  3. //
  4. // Note(!): This script will take first found Movie title, which in some cases
  5. // will mean it will take the wrong movie title. However, this script will
  6. // save you a ton of time when importing info for a large list. Just correct
  7. // the falsely imported infos manually later. Be sure to make a backup of your
  8. // list beforehand.
  9.  
  10. (****************************************************
  11.  *  Movie importation script for:                   *
  12.  *      IMDB (US), http://us.imdb.com               *
  13.  *                                                  *
  14.  *  (c) 2002-2004 Antoine Potten                    *
  15.  *                          software@antp.be        *
  16.  *  Contributors :                                  *
  17.  *    Danny Falkov                                  *
  18.  *    Kai Blankenhorn                               *
  19.  *    lboregard                                     *
  20.  *    Ork <ork@everydayangels.net>                  *
  21.  *    Trekkie <Asimov@hotmail.com>                  *
  22.  *  Batch-import improvements made by Youri Heijnen *
  23.  *                                                  *
  24.  *  For use with Ant Movie Catalog 3.4.0            *
  25.  *  www.antp.be/software/moviecatalog              *
  26.  *                                                 *
  27.  *  This program is free software; you can         *
  28.  *  redistribute it and/or modify it under the     *
  29.  *  terms of the GNU General Public License as     *
  30.  *  published by the Free Software Foundation;     *
  31.  *  either version 2 of the License, or (at your   *
  32.  *  option) any later version.                     *
  33.  ****************************************************)
  34.  
  35. program IMDb;
  36. const
  37.   // Set the following constants to True to import field, or False to skip field
  38.   ImportURL = True;
  39.   ImportOriginalTitle = True;
  40.     ImportTranslatedTitle = True;
  41.       LeaveOriginalTitle = True; // True will get Translated Title, yet Original Title field will remain same
  42.   ImportYear = True;
  43.   ImportRating = True;
  44.   ImportPicture = True;
  45.     ImportLargePicture = False; // If set to False small pic will be imported
  46.   ImportDirector = True;
  47.   ImportActors = True;
  48.   ImportCountry = True;
  49.   ImportCategory = True;
  50.   ImportDescription = True;
  51.     UseLongestDescription = False; // If set to False shortest description available will be imported, faster since taken from main page
  52.   ImportComments = True;
  53.   ImportLength = True;
  54.   ImportLanguage = True;
  55. const
  56.   ExternalPictures = False;
  57.     { True: Pictures will be stored as external files in the folder of the
  58.             catalog
  59.       False: Pictures will be stored inside the catalog (only for .amc files) }
  60.   ManualPictureSelect = False;
  61.     { True: If no Title Match found a picture selection window appears
  62.       False: Revert to IMDB picture }
  63. var
  64.   MovieName: string;
  65.   MovieURL: string;
  66.  
  67. function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer;
  68. var
  69.   i: Integer;
  70. begin
  71.   result := -1;
  72.   if StartAt < 0 then
  73.     StartAt := 0;
  74.   for i := StartAt to List.Count-1 do
  75.     if Pos(Pattern, List.GetString(i)) <> 0 then
  76.     begin
  77.       result := i;
  78.       Break;
  79.     end;
  80. end;
  81.  
  82. procedure AnalyzePage(Address: string);
  83. var
  84.   Page: TStringList;
  85.   LineNr: Integer;
  86.   MovieURL: string;
  87. begin
  88.   Page := TStringList.Create;
  89.   Page.Text := GetPage(Address);
  90.   if pos('<title>IMDb', Page.Text) = 0 then
  91.   begin
  92.     AnalyzeMoviePage(Page)
  93.   end
  94.   else
  95.   begin
  96.     MovieURL := AddMoviesTitles(Page, '<b>Exact Matches</b>');
  97.     if MovieURL = '' then
  98.       MovieURL := AddMoviesTitles(Page, '<b>Partial Matches</b>');
  99.     if MovieURL = '' then
  100.       MovieURL := AddMoviesTitles(Page, '<b>Approximate Matches</b>');
  101.     if MovieURL <> '' then
  102.       AnalyzePage(MovieURL);
  103.   end;
  104.   Page.Free;
  105. end;
  106.  
  107. function FindValue(BeginTag, EndTag: string; Page: TStringList; var LineNr: Integer; var Line: string): string;
  108. var
  109.   BeginPos, EndPos: Integer;
  110.   Value: string;
  111. begin
  112.   Result := '';
  113.   Value := '';
  114.   BeginPos := Pos(BeginTag, Line);
  115.   if BeginPos > 0 then
  116.   begin
  117.     BeginPos := BeginPos + Length(BeginTag);
  118.     if BeginTag = EndTag then
  119.     begin
  120.       Delete(Line,1,BeginPos-1);
  121.       BeginPos := 1;
  122.     end;
  123.     EndPos := pos(EndTag, Line);
  124.     while ((EndPos = 0) and (LineNr < Page.Count-1 )) do
  125.     begin
  126.       Value := Value + copy(Line, BeginPos, Length(Line) - BeginPos);
  127.       // Next Line
  128.       BeginPos := 1;
  129.       LineNr := LineNr + 1;
  130.       Line := Page.GetString(LineNr);
  131.       if Value = '' then
  132.         Exit;
  133.       EndPos := Pos(EndTag, Line);
  134.     end;
  135.     Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  136.    end;
  137.   Result := Value;
  138. end;
  139.  
  140. procedure AnalyzeMoviePage(Page: TStringList);
  141. var
  142.   Line, Value, Value2, FullValue, OldOriginalTitle: string;
  143.   LineNr, Desc, i: Integer;
  144.   BeginPos, EndPos: Integer;
  145.   OldTitleParts, AllTitles: TStringList;
  146.   LongDescr: Boolean;
  147. begin
  148.   LongDescr := UseLongestDescription;
  149.   if (LongDescr) and (Pos('<a href="plotsummary">', Page.Text) = 0) then
  150.     LongDescr := False;
  151.  
  152.   MovieURL := 'http://imdb.com/title/tt' + copy(Page.Text, pos('<a href="/title/tt',Page.Text)+19, 7);
  153.  
  154.   // URL
  155.   if ImportURL then
  156.     SetField(fieldURL, MovieURL);
  157.  
  158.   AllTitles := TStringList.Create;
  159.  
  160.   // Original Title & Year
  161.   if (ImportOriginalTitle) or (ImportYear) then
  162.   begin
  163.     LineNr := FindLine('<title>', Page, 0);
  164.     Line := Page.GetString(LineNr);
  165.     if LineNr > -1 then
  166.     begin
  167.       BeginPos := pos('<title>', Line);
  168.       if BeginPos > 0 then
  169.         BeginPos := BeginPos + 7;
  170.       EndPos := pos('(', Line);
  171.       if EndPos = 0 then
  172.         EndPos := Length(Line);
  173.       Value := copy(Line, BeginPos, EndPos - BeginPos - 1);
  174.       HTMLDecode(Value);
  175.       if ImportOriginalTitle then
  176.         OldOriginalTitle := GetField(fieldOriginalTitle);
  177.       if (ImportTranslatedTitle) and not (LeaveOriginalTitle) then
  178.         SetField(fieldOriginalTitle, Value);
  179.       BeginPos := pos('(', Line) + 1;
  180.       if BeginPos > 0 then
  181.       begin
  182.         EndPos := Pos('/I', Line);
  183.         if EndPos < BeginPos then
  184.           EndPos := pos(')', Line);
  185.         Value := copy(Line, BeginPos, EndPos - BeginPos);
  186.         if ImportYear then
  187.           SetField(fieldYear, Value);
  188.       end;
  189.     end;
  190.   end;
  191.  
  192.   // Translated Title
  193.   if ImportTranslatedTitle then
  194.   begin
  195.     OldTitleParts := TStringList.Create;
  196.     // Tokenize OldOriginalTitle while removing certain chars/common words ("the", "of")
  197.     Value := AnsiUpperCase(OldOriginalTitle);
  198.     Value := StringReplace(StringReplace(Value, ',', ' '), ':', ' ');
  199.     Value := StringReplace(StringReplace(Value, '(', ' '), ')', ' ');
  200.     Value := StringReplace(StringReplace(Value, 'OF', ' '), 'THE', ' ');
  201.     repeat
  202.       Value := StringReplace(Value, '  ', ' ');
  203.     until Pos('  ', Value) = 0;
  204.     Value := StringReplace(Trim(Value), ' ', ',');
  205.     // Value now contains the original title (comma-separated) that was filled in before running the script
  206.     Value2 := '';
  207.     for i := 1 to Length(Value) do
  208.     begin
  209.       if Pos(',', Copy(Value, i, 1)) = 0 then
  210.         Value2 := Value2 + Copy(Value, i, 1);
  211.       if (Pos(',', Copy(Value, i, 1)) = 1) or (i = Length(Value)) then
  212.       begin
  213.         OldTitleParts.Add(Value2); // put each comma-separated value from Value into a separate string in TitleParts
  214.         Value2 := '';
  215.       end;
  216.     end;
  217.     for i := 0 to OldTitleParts.Count - 1 do
  218.     // Begin comparing title parts (from the title originally filled in by moviedb owner) with
  219.     // the 'true' Original Title (extracted from IMDb) to see if it's a foreign title and needs a Translated Title
  220.     begin
  221.       if Pos(OldTitleParts.GetString(i), AnsiUpperCase(GetField(fieldOriginalTitle))) <= 0 then
  222.       begin // no match, must be a foreign title
  223.         LineNr := FindLine('Also Known As', Page, 0);
  224.         if LineNr > -1 then
  225.         begin
  226.           Line := Page.GetString(LineNr);
  227.           if Pos('Also Known As', Line) > 0 then
  228.           begin
  229.             BeginPos := Pos('Also Known As', Line) + 26;
  230.             Value := Copy(Line, BeginPos, Length(Line) - BeginPos - 4);
  231.             Value := StringReplace(Value, '<br>', '/ ');
  232.             HTMLDecode(Value);
  233.             SetField(fieldTranslatedTitle, Trim(Value));
  234.           end;
  235.         end;
  236.         Break;
  237.       end;
  238.     end;
  239.     OldTitleParts.Free;
  240.   end;
  241.  
  242.   // Rating
  243.   if ImportRating then
  244.   begin
  245.     LineNr := FindLine('User Rating:', Page, 0);
  246.     if LineNr > -1 then
  247.     begin
  248.       Line := Page.GetString(LineNr + 4);
  249.       if Pos('/10', Line) > 0 then
  250.       begin
  251.         BeginPos := pos('<b>', Line) + 3;
  252.         Value := IntToStr(Round(StrToInt(StrGet(Line, BeginPos), 0) + (StrToInt(StrGet(Line, BeginPos + 2), 0) / 10)));
  253.         SetField(fieldRating, Value);
  254.       end;
  255.     end;
  256.   end;
  257.  
  258.   // Language
  259.   LineNr := FindLine('Language:', Page, 0);
  260.   if LineNr > -1 then
  261.   begin
  262.     Line := Page.GetString(LineNr + 1);
  263.     BeginPos := pos('/">', Line) + 3;
  264.     EndPos := pos('</a>', Line);
  265.     if EndPos = 0 then
  266.       EndPos := Length(Line);
  267.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  268.     if ImportLanguage then
  269.       SetField(fieldLanguages, Value);
  270.   end;
  271.  
  272.   if ImportPicture then
  273.     GetMoviePicture(Value, Page, AllTitles);
  274.   AllTitles.Free;
  275.  
  276.   // Director
  277.   if ImportDirector then
  278.   begin
  279.     LineNr := FindLine('Directed by', Page, 0);
  280.     if LineNr > -1 then
  281.     begin
  282.       FullValue := '';
  283.       Line := Page.GetString(LineNr + 1);
  284.       repeat
  285.         BeginPos := pos('">', Line) + 2;
  286.         EndPos := pos('</a>', Line);
  287.         Value := copy(Line, BeginPos, EndPos - BeginPos);
  288.         if (Value <> '(more)') and (Value <> '') then
  289.         begin
  290.           if FullValue <> '' then
  291.             FullValue := FullValue + ', ';
  292.           FullValue := FullValue + Value;
  293.         end;
  294.         Delete(Line, 1, EndPos);
  295.       until Pos('</a>', Line) = 0;
  296.       HTMLDecode(FullValue);
  297.       SetField(fieldDirector, FullValue);
  298.     end;
  299.   end;
  300.  
  301.   // Actors
  302.   if ImportActors then
  303.   begin
  304.     LineNr := FindLine('ast overview', Page, 0);
  305.     if LineNr = -1 then
  306.       LineNr := FindLine('redited cast', Page, 0);
  307.     if LineNr > -1 then
  308.     begin
  309.       FullValue := '';
  310.       Line := Page.GetString(LineNr);
  311.       repeat
  312.         BeginPos := Pos('<td valign="top">', Line);
  313.         if BeginPos > 0 then
  314.         begin
  315.           Delete(Line, 1, BeginPos);
  316.           Line := copy(Line, 25, Length(Line));
  317.           BeginPos := pos('">', Line) + 2;
  318.           EndPos := pos('</a>', Line);
  319.           if EndPos = 0 then
  320.             EndPos := Pos('</td>', Line);
  321.           Value := copy(Line, BeginPos, EndPos - BeginPos);
  322.           if (Value <> '(more)') and (Value <> '') then
  323.           begin
  324.             BeginPos := pos('.... </td><td valign="top">', Line);
  325.             if BeginPos > 0 then
  326.             begin
  327.               EndPos := pos('</td></tr>', Line);
  328.               BeginPos := BeginPos + 27;
  329.               Value2 := copy(Line, BeginPos, EndPos - BeginPos);
  330.               if Value2 <> '' then
  331.               begin
  332.                 Value := Value + ' (as ' + Value2 + ')';
  333.               end;
  334.             end;
  335.             if FullValue <> '' then
  336.               FullValue := FullValue + ', ';
  337.             FullValue := FullValue + Value;
  338.           end;
  339.           EndPos := Pos('</td></tr>', Line);
  340.           Delete(Line, 1, EndPos);
  341.         end else
  342.         begin
  343.           Line := '';
  344.         end;
  345.       until Line = '';
  346.       HTMLDecode(FullValue);
  347.       SetField(fieldActors, FullValue);
  348.     end;
  349.   end;
  350.  
  351.   // Country
  352.   if ImportCountry then
  353.   begin
  354.     LineNr := FindLine('Country:', Page, 0);
  355.     if LineNr > -1 then
  356.     begin
  357.       Line := Page.GetString(LineNr + 1);
  358.       BeginPos := pos('/">', Line) + 3;
  359.       EndPos := pos('</a>', Line);
  360.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  361.       HTMLDecode(Value);
  362.       SetField(fieldCountry, Value);
  363.     end;
  364.   end;
  365.  
  366.   // Category
  367.   if ImportCategory then
  368.   begin
  369.     LineNr := FindLine('Genre:', Page, 0);
  370.     if LineNr > -1 then
  371.     begin
  372.       Line := Page.GetString(LineNr + 1);
  373.       BeginPos := pos('/">', Line) + 3;
  374.       EndPos := pos('</a>', Line);
  375.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  376.       HTMLDecode(Value);
  377.       SetField(fieldCategory, Value);
  378.     end;
  379.   end;
  380.  
  381.   //Description
  382.   if ImportDescription then
  383.   begin
  384.     LineNr := FindLine('Plot Summary:', Page, 0);
  385.     if LineNr < 1 then
  386.       LineNr := FindLine('Plot Outline:', Page, 0);
  387.     if LineNr > -1 then
  388.     begin
  389.       Line := Page.GetString(LineNr);
  390.       BeginPos := pos('</b>', Line) + 5;
  391.       EndPos := pos('<a href', Line);
  392.       if EndPos < 1 then
  393.       begin
  394.         Line := Line + Page.GetString(LineNr+1);
  395.         EndPos := pos('<br><br>', Line);
  396.         if EndPos < 1 then
  397.           EndPos := Length(Line);
  398.       end;
  399.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  400.       HTMLDecode(Value);
  401.       if UseLongestDescription then
  402.         SetField(fieldDescription, GetDescriptions(MovieURL + 'plotsummary'))
  403.       else
  404.         SetField(fieldDescription, Value);
  405.     end;
  406.   end;
  407.  
  408.   // Comments
  409.   if ImportComments then
  410.   begin
  411.     LineNr := FindLine('<b>Summary:</b>', Page, 0);
  412.     if LineNr > -1 then
  413.     begin
  414.       Value := '';
  415.       repeat
  416.         LineNr := LineNr + 1;
  417.         Line := Page.GetString(LineNr);
  418.         EndPos := Pos('</blockquote>', Line);
  419.         if EndPos = 0 then
  420.           EndPos := Length(Line)
  421.         else
  422.           EndPos := EndPos - 1;
  423.         Value := Value + Copy(Line, 1, EndPos) + ' ';
  424.       until Pos('</blockquote>', Line) > 0;
  425.       HTMLDecode(Value);
  426.       Value := StringReplace(Value, '<br>', #13#10);
  427.       Value := StringReplace(Value, #13#10+' ', #13#10);
  428.       SetField(fieldComments, Value);
  429.     end;
  430.   end;
  431.  
  432.   // Length
  433.   if ImportLength then
  434.   begin
  435.     LineNr := FindLine('Runtime:', Page, 0);
  436.     if LineNr > -1 then
  437.     begin
  438.       Line := Page.GetString(LineNr + 1);
  439.       EndPos := pos(' min', Line);
  440.       if EndPos = 0 then
  441.         EndPos := pos('  /', Line);
  442.       if EndPos = 0 then
  443.         EndPos := Length(Line);
  444.       if Pos(':', Line) < EndPos then
  445.         BeginPos := Pos(':', Line) + 1
  446.       else
  447.         BeginPos := 1;
  448.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  449.       SetField(fieldLength, Value);
  450.     end;
  451.   end;
  452.  
  453.   DisplayResults;
  454. end;
  455.  
  456. procedure GetMoviePicture(Language: string; Page, AllTitles: TStringList);
  457. var
  458.   Line, Value, Value2, Aka, PictureAddress: string;
  459.   AmazonPage: TStringList;
  460.   FoundOnAmazon, PickTreeSelected, PictureAvailable: Boolean;
  461.   TitleRef, ImgRef, NoImage: string;
  462.   LineNr, BeginPos, EndPos, PickTreeCount, ParagraphIndex, Index, TitleLine, LastMatch: Integer;
  463. begin
  464.   FoundOnAmazon := False;
  465.  
  466.   if ImportLargePicture then
  467.   begin
  468.     // Find Alternate Titles for Movies which are not in English
  469.     Aka := '';
  470.     if Language <> 'English' Then
  471.     begin
  472.       LineNr:= FindLine('Also Known As',Page,0);
  473.       EndPos:=0;
  474.       if LineNr > -1 then
  475.       begin
  476.         Line := Page.GetString(LineNr);
  477.         repeat
  478.           Aka:=FindValue('<br>','<br>',Page,LineNr,Line);
  479.           if Aka <> '' then
  480.           begin
  481.             BeginPos:=1;
  482.             EndPos:=Pos('(',Line);
  483.             if EndPos = 0 then
  484.               EndPos := Length(Aka);
  485.             Value := copy(Aka, BeginPos, EndPos - BeginPos - 1);
  486.             Value:=TransFormIMDBTitle(Value);
  487.             AllTitles.Add(Value);
  488.           end;
  489.         until (Pos('Runtime',Line) > 0) or (Pos('MPAA',Line) > 0 );
  490.       end;
  491.     end;
  492.  
  493.     TitleRef:='dvd>';
  494.     ImgRef:='dvd><img';
  495.     NoImage:='/icons/dvd-no-image.gif';
  496.     LineNr := FindLine('title="DVD available at Amazon.com"', Page, 0);
  497.     if LineNr = -1 then
  498.     begin
  499.       LineNr := FindLine('title="VHS available at Amazon.com"', Page, 0);
  500.       if LineNr > -1 then
  501.       begin
  502.         TitleRef:='video>';
  503.         ImgRef:='video><img';
  504.         NoImage:='/icons/video-no-image.gif';
  505.       end;
  506.     end;
  507.  
  508.     if LineNr > -1 then
  509.     begin
  510.       Line := Page.GetString(LineNr);
  511.       if(TitleRef='dvd>') then
  512.       begin
  513.         EndPos := pos('title="DVD', Line);
  514.         BeginPos := pos('title="VHS', Line);
  515.         while (BeginPos > 0) and (BeginPos<EndPos) do
  516.         begin
  517.           Delete(Line, 1, BeginPos+1);
  518.           BeginPos := pos('title="VHS', Line);
  519.         end;
  520.       end;
  521.       BeginPos := Pos('href="', Line) + 5;
  522.       Delete(Line, 1, BeginPos);
  523.       EndPos := Pos('"', Line);
  524.       Value := Copy(Line, 1, EndPos - 1);
  525.       AmazonPage := TStringList.Create;
  526.       AmazonPage.Text := GetPage('http://us.imdb.com' + Value);
  527.  
  528.       // Original Title
  529.       Value2 := AllTitles.GetString(0);
  530.       Value2 := TransFormIMDBTitle(Value2);
  531.   
  532.       PickTreeClear;
  533.       PickTreeCount := 0;
  534.       PickTreeAdd('Available Titles for matching a picture to: ' + Value2, '');
  535.   
  536.       ParagraphIndex := 1;
  537.       LineNr := 0;
  538.       LastMatch := -1;
  539.       TitleLine := -1;
  540.       repeat
  541.         LineNr := FindLine('<b>'+IntToStr(ParagraphIndex)+'.', AmazonPage, LineNr);
  542.   
  543.         if LineNr > -1 then
  544.         begin
  545.           TitleLine:=LineNr;
  546.           Value:='';
  547.           PictureAvailable:=False;
  548.           repeat
  549.             TitleLine:=TitleLine +1;
  550.             Line:= AmazonPage.GetString(TitleLine);
  551.             BeginPos:=0;
  552.             if Pos(TitleRef,Line) > 0 then
  553.             begin
  554.               if Pos(ImgRef,Line) = 0 then
  555.               begin
  556.                 for Index:=0 to AllTitles.Count -1 do
  557.                 begin
  558.                   Value2:=AllTitles.GetString(Index);
  559.                   BeginPos:=Pos(Value2,Line);
  560.                   if BeginPos > 0 then
  561.                     Break;
  562.                 end;
  563.                 // Match not found
  564.                 if BeginPos = 0 then
  565.                 begin
  566.                   BeginPos:=Pos(TitleRef,Line)+Length(TitleRef);
  567.                   EndPos:=Pos('</a>',Line);
  568.                   Value:=Copy(Line,BeginPos,EndPos-BeginPos);
  569.                 end;
  570.               end
  571.               else
  572.               begin
  573.                 PictureAvailable:=(Pos(NoImage,Line) = 0);
  574.                 PictureAddress:=IntToStr(TitleLine);
  575.               end;
  576.             end;
  577.             if BeginPos > 0 then
  578.               Break;
  579.           until (Pos('</table>',Line ) > 0);
  580.  
  581.           // Try to Find a Title Match
  582.           if Pos(Value2,Line) > 0 then
  583.           begin
  584.             // Compare Current Title to Original
  585.             BeginPos := Pos(TitleRef, Line) + Length(TitleRef) -1;
  586.             Delete(Line, 1, BeginPos);
  587.             EndPos:= Pos('(',Line);
  588.             if EndPos = 0 Then
  589.               EndPos := Pos('</a>', Line);
  590.             Value := Copy(Line, 1, EndPos - 1);
  591.             Value:= Trim(Value);
  592.             if Value = Value2 then
  593.             begin
  594.               if PictureAvailable then
  595.                 LastMatch:=LineNr;
  596.                 //Break
  597.             end;
  598.           end;
  599.           if PictureAvailable then
  600.           begin
  601.             PickTreeAdd(Value,PictureAddress);
  602.             PickTreeCount:=PickTreeCount+1;
  603.           end;
  604.         end;
  605.         ParagraphIndex:=ParagraphIndex+1;
  606.       until (LineNr = -1);
  607.       LineNr:=LastMatch;
  608.       if (LineNr = -1) then
  609.       begin
  610.         // Handle Amazon Page Redirection(s)
  611.         LineNr:= FindLine('You clicked on this item',AmazonPage,0);
  612.         if (LineNr = -1) then
  613.           LineNr:=FindLine('Customers who bought',AmazonPage,0);
  614.         // Display the Picture Selection Window
  615.         if (LineNr = -1) and ManualPictureSelect and (PickTreeCount > 0) then
  616.         begin
  617.           PickTreeSelected:=PickTreeExec(PictureAddress);
  618.           if PickTreeSelected then
  619.             LineNr:=StrToInt(PictureAddress,0);
  620.         end;
  621.         if (LineNr > -1 ) then
  622.         begin
  623.           LineNr := FindLine('src="http://images.amazon.com/images/P/',AmazonPage, LineNr);
  624.           if not PickTreeSelected then
  625.             TitleLine:= FindLine('/exec/obidos/ASIN/',AmazonPage, 0);
  626.           if (LineNr > TitleLine) then
  627.             LineNr:=-1
  628.           if LineNr > -1 then
  629.           begin
  630.             Line := AmazonPage.GetString(LineNr);
  631.             BeginPos := Pos('src="http://images.amazon.com/images/P/', Line) + 4;
  632.             Delete(Line, 1, BeginPos);
  633.             EndPos := Pos('"', Line);
  634.             Value := Copy(Line, 1, EndPos - 1);
  635.             Value := StringReplace(Value, 'TZZZZZZZ', 'LZZZZZZZ');
  636.             Value := StringReplace(Value, 'THUMBZZZ', 'LZZZZZZZ');
  637.             GetPicture(Value, ExternalPictures);
  638.             FoundOnAmazon := True;
  639.           end;
  640.         end;
  641.       end
  642.       else
  643.       begin
  644.         LineNr := FindLine('http://images.amazon.com/images/P/', AmazonPage, LineNr);
  645.         if LineNr < TitleLine then
  646.         begin
  647.           Line := AmazonPage.GetString(LineNr);
  648.           BeginPos := Pos('src="', Line) + 4;
  649.           Delete(Line, 1, BeginPos);
  650.           EndPos := Pos('"', Line);
  651.           Value := Copy(Line, 1, EndPos - 1);
  652.           Value := StringReplace(Value, 'THUMBZZZ', 'LZZZZZZZ');
  653.           GetPicture(Value, ExternalPictures);
  654.           FoundOnAmazon := True;
  655.         end;
  656.       end;
  657.       AmazonPage.Free;
  658.     end;
  659.   end; // if ImportLargePicture
  660.  
  661.   if not FoundOnAmazon then
  662.   begin
  663.     {  not found on Amazon, so taking what's available directly on IMDB.
  664.        if we are lucky, a picture from amazon but directly linked in the page  }
  665.     LineNr := FindLine('<img alt="cover" align="left" src="http://ia.imdb.com/media/imdb/', Page, 0);
  666.     if LineNr < 0 then
  667.       LineNr := FindLine('<img alt="cover" align="left" src="http://posters.imdb.com/', Page, 0);
  668.     if LineNr < 0 then
  669.       LineNr := FindLine('<img alt="cover" align="left" src="http://images.amazon.com/', Page, 0);
  670.     if LineNr > -1 then
  671.     begin
  672.       Line := Page.GetString(LineNr);
  673.       BeginPos := pos('src="', Line) + 4;
  674.       Delete(Line, 1, BeginPos);
  675.       EndPos := pos('"', Line);
  676.       Value := copy(Line, 1, EndPos - 1);
  677.       Value := StringReplace(Value, 'MZZZZZZZ', 'LZZZZZZZ'); // change URL to get the Large instead of Small image
  678.       GetPicture(Value, ExternalPictures);
  679.     end;
  680.   end;
  681. end;
  682.  
  683. function TransformIMDBTitle(Title: string): string;
  684. var
  685.   BeginPos, EndPos: Integer;
  686.   Value: string;
  687.   Words: array of string;
  688.   Articles: array of string;
  689.   Replace,Original: string;
  690.   Index, CommaCount: Integer;
  691. Begin
  692.   // Original Title
  693.   Result:=Title;
  694.  
  695.   Setarraylength(Words,11);
  696.   Words[0]:=' In ';
  697.   Words[1]:=' On ';
  698.   Words[2]:=' Of ';
  699.   Words[3]:=' As ';
  700.   Words[4]:=' The ';
  701.   Words[5]:=' At ';
  702.   Words[6]:=' And A ';
  703.   Words[7]:=' And ';
  704.   Words[8]:=' An ';
  705.   Words[9]:=' To ';
  706.   Words[10]:=' For ';
  707.  
  708.   SetArrayLength(Articles,35);
  709.   Articles[0]:=' The';
  710.   Articles[1]:=' a';
  711.   Articles[2]:=' An';
  712.   Articles[3]:=' Le';
  713.   Articles[4]:=' L''';
  714.   Articles[5]:=' Les';
  715.   Articles[6]:=' Der';
  716.   Articles[7]:=' Das';
  717.   Articles[8]:=' Die';
  718.   Articles[9]:=' Des';
  719.   Articles[10]:=' Dem';
  720.   Articles[11]:=' Den';
  721.   Articles[12]:=' Ein';
  722.   Articles[13]:=' Eine';
  723.   Articles[14]:=' Einen';
  724.   Articles[15]:=' Einer';
  725.   Articles[16]:=' Eines';
  726.   Articles[17]:=' Einem';
  727.   Articles[18]:=' Il';
  728.   Articles[19]:=' Lo';
  729.   Articles[20]:=' La';
  730.   Articles[21]:=' I';
  731.   Articles[22]:=' Gli';
  732.   Articles[23]:=' Le';
  733.   Articles[24]:=' Uno';
  734.   Articles[25]:=' Una';
  735.   Articles[26]:=' Un''';
  736.   Articles[27]:=' O';
  737.   Articles[28]:=' Os';
  738.   Articles[29]:=' As';
  739.   Articles[30]:=' El';
  740.   Articles[31]:=' Los';
  741.   Articles[32]:=' Las';
  742.   Articles[33]:=' Unos';
  743.   Articles[34]:=' Unas';
  744.  
  745.   // Count the Comma in The Title
  746.   CommaCount := 0;
  747.   EndPos := 0;
  748.   Value := Title;
  749.   repeat
  750.      BeginPos := Pos(',', Value);
  751.      if BeginPos > 0 then
  752.      begin
  753.        Delete(Value, 1, BeginPos);
  754.        CommaCount := CommaCount + 1;
  755.        EndPos := EndPos + BeginPos;
  756.      end;
  757.   until( Pos(',',Value) = 0);
  758.  
  759.   // Compare the Article to a list of known ones
  760.   for Index := 0 to 34 do
  761.   begin
  762.     if Pos(Articles[Index], Value) <> 0 then
  763.     begin
  764.        CommaCount := 1;
  765.        BeginPos := EndPos;
  766.        Break;
  767.     end;
  768.   end;
  769.  
  770.   if (BeginPos > 0) and (CommaCount = 1) then
  771.   begin
  772.     Value := Copy(Title, BeginPos + 1, Length(Title));
  773.     Value := Trim(Value);
  774.     Result := Value + ' ' + Copy(Title, 1, BeginPos - 1);
  775.   end;
  776.  
  777.   BeginPos := Pos(': ', Result);
  778.   if BeginPos > 0 then
  779.     Result := StringReplace(Result, ': ', ' - ');
  780.  
  781.   Result := AnsiMixedCase(Result, ' ');
  782.  
  783.   for Index := 0 to 10 do
  784.   begin
  785.     if Pos(Words[Index],Result) <> 0 then
  786.     begin
  787.       Original := Words[Index];
  788.       Replace := AnsiLowerCase(Original);
  789.       Result := StringReplace(Result, Original, Replace);
  790.     end;
  791.   end;
  792.  
  793.   Result := StringReplace(Result, ' - the ', ' - The ');
  794.   Result := Trim(Result);
  795. end;
  796.  
  797. function GetDescriptions(Address: string): string;
  798. var
  799.   Line, Value: string;
  800.   LineNr: Integer;
  801.   BeginPos, EndPos,Longest: Integer;
  802.   Page: TStringList;
  803. begin
  804.   Result := '';
  805.   Longest := 0;
  806.   Page := TStringList.Create;
  807.   Page.Text := GetPage(Address);
  808.   LineNr := FindLine('<p class="plotpar">', Page, 0);
  809.   while LineNr > -1 do
  810.   begin
  811.     Value := '';
  812.     repeat
  813.       Line := Page.GetString(LineNr);
  814.       BeginPos := pos('"plotpar">', Line);
  815.       if BeginPos > 0 then
  816.         BeginPos := BeginPos + 10
  817.       else
  818.         BeginPos := 1;
  819.       EndPos := pos('</p>', Line);
  820.       if EndPos < 1 then
  821.         EndPos := Length(Line) + 1;
  822.       if Value <> '' then
  823.         Value := Value + ' ';
  824.       Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  825.       LineNr := LineNr + 1;
  826.     until (pos('</p>', Line) > 0) or (LineNr = Page.Count);
  827.     HTMLDecode(Value);
  828.     PickListAdd(Value);
  829.  
  830.     if Length(Value) > Longest then
  831.     begin
  832.       Result := Value;
  833.       Longest := Length(Value);
  834.     end;
  835.  
  836.     LineNr := FindLine('<p class="plotpar">', Page, LineNr);
  837.   end;
  838.   Page.Free;
  839. end;
  840.  
  841. function AddMoviesTitles(Page: TStringList; Tag: string): string;
  842. var
  843.   Line: string;
  844.   LineNr: Integer;
  845.   StartPos: Integer;
  846. begin
  847.   Result := '';
  848.   LineNr := FindLine(tag, Page, 0);
  849.   if LineNr > -1 then
  850.   begin
  851.     Line := Page.GetString(LineNr);
  852.     HTMLRemoveTags(Line);
  853.     PickTreeAdd(Trim(Line), '');
  854.     LineNr := LineNr + 5;
  855.     Line := Page.GetString(LineNr);
  856.     StartPos := pos('href="', Line) + 5;
  857.     Delete(Line, 1, StartPos);
  858.     Result := Copy(Line, 1, pos('">', Line) - 1);
  859.   end;
  860. end;
  861.  
  862. begin
  863.   if CheckVersion(3,4,0) then
  864.   begin
  865.     MovieName := GetField(fieldOriginalTitle);
  866.     if MovieName = '' then
  867.       MovieName := GetField(fieldTranslatedTitle);
  868.     if (MovieName = '') then
  869.       if not Input('IMDb Import', 'Enter the title of the movie:', MovieName) then
  870.         Exit;
  871.     AnalyzePage('http://us.imdb.com/Tsearch?title='+UrlEncode(MovieName));
  872.   end else
  873.     ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');
  874. end.
  875.